﻿namespace SERFLITEMAX20.Viewer
{
    using SERFLITEMAX20;
    using SERFLITEMAX20.DataObjects;
    using System;
    using System.Xml;

    public class ViewObject
    {
        public long channel;
        public long charm;
        public int entry;
        public float facing;
        public int flags;
        public long guid;
        internal bool invalid;
        public bool isDead;
        public int level;
        public string name;
        public enNodeType nodeType;
        public bool special;
        public long summon;
        public long target;
        public enObjectTypes type;
        public GameobjectTypes typeid;
        public int unitReaction;
        public float x;
        public float y;
        public float z;

        internal ViewObject(long pGuid)
        {
            this.invalid = false;
            this.type = enObjectTypes.None;
            this.nodeType = enNodeType.None;
            this.typeid = GameobjectTypes.NONE;
            this.guid = 0L;
            this.entry = 0;
            this.facing = 0f;
            this.name = null;
            this.special = false;
            this.flags = 0;
            this.level = 0;
            this.unitReaction = 0;
            this.isDead = false;
            this.target = 0L;
            this.channel = 0L;
            this.summon = 0L;
            this.charm = 0L;
            this.guid = pGuid;
        }

        internal ViewObject(XmlElement pElement)
        {
            this.invalid = false;
            this.type = enObjectTypes.None;
            this.nodeType = enNodeType.None;
            this.typeid = GameobjectTypes.NONE;
            this.guid = 0L;
            this.entry = 0;
            this.facing = 0f;
            this.name = null;
            this.special = false;
            this.flags = 0;
            this.level = 0;
            this.unitReaction = 0;
            this.isDead = false;
            this.target = 0L;
            this.channel = 0L;
            this.summon = 0L;
            this.charm = 0L;
            this.guid = pElement.GetAttribute("guid").ToLong();
            this.Update(pElement);
        }

        public float GetDistance(ViewObject obj)
        {
            if (obj == null)
            {
                return 0f;
            }
            return this.GetDistance(obj.x, obj.y, obj.z);
        }

        public float GetDistance(float x, float y, float z)
        {
            float num = this.x - x;
            float num2 = this.y - y;
            float num3 = (z != 0f) ? (this.z - z) : 0f;
            return (float) Math.Sqrt((double) (((num * num) + (num2 * num2)) + (num3 * num3)));
        }

        public float GetFaceRadian(float faceX, float faceY)
        {
            float num = this.x - faceX;
            float num2 = faceY - this.y;
            float num3 = 0f;
            if ((num > 0.0) && (num2 >= 0.0))
            {
                num3 = (float) Math.Atan((double) (num2 / num));
            }
            if ((num < 0.0) && (num2 >= 0.0))
            {
                num3 = ((float) Math.Atan((double) (num2 / num))) + 3.141593f;
            }
            if ((num < 0.0) && (num2 < 0.0))
            {
                num3 = ((float) Math.Atan((double) (num2 / num))) + 3.141593f;
            }
            if ((num > 0.0) && (num2 < 0.0))
            {
                num3 = ((float) Math.Atan((double) (num2 / num))) + 6.283185f;
            }
            return (num3 - 1.570796f);
        }

        public FilterObject GetFilterObject()
        {
            FilterObject obj2 = new FilterObject();
            obj2.name = this.name;
            obj2.key = this.type + "_" + this.entry;
            obj2.type = this.type.ToString();
            if (obj2.type == "GameObject")
            {
                obj2.typeid = this.typeid.ToString();
            }
            else
            {
                obj2.typeid = "";
            }
            obj2.entry = this.entry;
            obj2.typeid = this.typeid.ToString();
            obj2.filtered = false;
            obj2.special = false;
            if (obj2.typeid.Length > 0)
            {
                obj2.group = obj2.typeid;
                return obj2;
            }
            obj2.group = "General";
            return obj2;
        }

        public void Update(XmlElement pElement)
        {
            if (pElement.GetAttribute("HasChanged", true))
            {
                this.entry = pElement.GetAttribute("entry", this.entry);
                this.type = pElement.GetAttribute("type", this.type);
                this.name = pElement.GetAttribute("name");
                this.x = pElement.GetAttribute("x", this.x);
                this.y = pElement.GetAttribute("y", this.y);
                this.z = pElement.GetAttribute("z", this.z);
                this.facing = pElement.GetAttribute("facing", this.facing);
                if (((this.type == enObjectTypes.Player) || (this.type == enObjectTypes.Corpse)) || (this.type == enObjectTypes.Unit))
                {
                    this.flags = pElement.GetAttribute("flags", this.flags);
                    this.isDead = pElement.GetAttribute("isdead", this.isDead);
                    if (this.type != enObjectTypes.Corpse)
                    {
                        this.level = pElement.GetAttribute("level", this.level);
                        this.unitReaction = pElement.GetAttribute("reaction", this.unitReaction);
                    }
                    this.target = pElement.GetAttribute("target", this.target);
                    this.channel = pElement.GetAttribute("channel", this.channel);
                    this.summon = pElement.GetAttribute("summon", this.summon);
                    this.charm = pElement.GetAttribute("charm", this.charm);
                }
                else if (this.type == enObjectTypes.GameObject)
                {
                    this.typeid = pElement.GetAttribute("typeid", this.typeid);
                    this.nodeType = pElement.GetAttribute("nodetype", this.nodeType);
                }
            }
        }

        public bool ValidFilter()
        {
            return ((!this.name.IsEmpty() && (this.entry > 0)) && (this.guid > 0L));
        }

        public string key
        {
            get
            {
                return (this.type + "_" + this.entry);
            }
        }

        public string LongName
        {
            get
            {
                if (this.type == enObjectTypes.GameObject)
                {
                    return string.Concat(new object[] { this.name, " typeid: ", this.typeid, " nodetype: ", this.nodeType, " entry: ", this.entry, " flags: ", this.flags });
                }
                return string.Concat(new object[] { this.name, " entry: ", this.entry, " flags: ", this.flags });
            }
        }
    }
}

